package com.mid;

/**
 * Created by Lxk on 2020/1/31.
 */
public class Solution134 {

    //单个位置判断是否可以返回
    private boolean canReturn(int[] gas, int[] cost, int pos) {
        int num = gas.length;
        int remain = 0;
        for (int i = 0; i < num; i++) {
            int curPos = pos + i;
            curPos = curPos % num;
            if (gas[curPos] + remain < cost[curPos]) {
                return false;
            }
            remain = gas[curPos] + remain - cost[curPos];
        }
        if (remain + gas[pos] >= cost[pos]) {
            return true;
        } else {
            return false;
        }
    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        //寻找一个起点
        for (int i = 0; i < gas.length; i++) {
            //一个一个作为七点进行判断
            if (canReturn(gas, cost, i)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] gas = {2};
        int[] cost = {2};
        Solution134 solution134 = new Solution134();
        System.out.println(solution134.canCompleteCircuit(gas, cost));
    }

}
